"""
对比 强弱；
spread
        ss_config["data"]= {  # 核心是准备计算各种基础数据，方便计算的一个框架,ontick,onbar下执行
            self.pcode: {
                self.datatype: {
                    "args": {},  # kline初始化参数,tick时有效
                    "factor": [],  # 读取obj  k1m周期的因子并合并
                    "calcors": self.args_template["signal_calcors"],
                },
            },
            "spread":[
            {"name":XXX,","class":"comparison",init_args:{},"out":"act"}
            ]
        }
"""
from mohe.core.obj import obj_trade
class comparison:
    def __init__(self,objs,out="comparison"):
        self.objs=objs
        self.obj_trade0=obj_trade(objs[0])
        self.data={}
        for i in objs:
            self.data[i]=[None,None]
        self.len=len(self.data)-1
        self.out=out
        self.base_obj=objs[-1]
        if len(self.objs)==2:
            self.islist=False
        else:
            self.islist=True
    def onbar(self,obj_key,period,b,data):
        self.data[obj_key][0]=b["close"]
        n=int(period[1:-1])
        timekey=b["timekey"]
        if obj_key==self.objs[0]:
            if self.data[obj_key]==2:
                base=self.data[self.base_obj]
                if len(base)==2:
                    r0=(base[0]-base[1])/base[1]
                    rs=[]
                    for i in self.objs[:self.len]:
                        if len(i)==2:
                            r1=(i[0]-i[1])/i[1]
                            rs.append(r1)
                        else:
                            rs.append(None)
                    if len(rs)==1:
                        b[self.out] =rs[0]-r0
                    else:
                        rs0=[]
                        for i in rs:
                            rs0.append(i-r0)
                        b[self.out] = rs0
                else:
                    if self.islist:
                        rs0 = []
                        for i in self.objs[:self.len]:
                            rs0.append(None)
                        b[self.out] = rs0
                    else:
                        b[self.out] = None
            else:
                if self.islist:
                    rs0=[]
                    for i in self.objs[:self.len]:
                        rs0.append(None)
                    b[self.out] =rs0
                else:
                    b[self.out]=None
        c = self.obj_trade0.isovernight(timekey,n)
        if c[0]:
            self.data[obj_key][1] = b["close"]


